package com.lzmpt.imchat.ui.chatactivity;

import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.widget.EaseAlertDialog;
import com.lzmpt.imchat.DemoHelper;
import com.lzmpt.imchat.R;
import com.lzmpt.imchat.ui.BaseActivity;

/**
 * 填写并发送添加请求
 */
public class AddContactMsgActivity extends BaseActivity {

    private String toAddUsername;
    private ProgressDialog progressDialog;

    private EditText mContent;
    private EditText mMsg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_contact_msg);
        mContent = findViewById(R.id.add_contact_content);
        mMsg = findViewById(R.id.add_contact_msg);
        toAddUsername = getIntent().getStringExtra("userId");
    }

    /**
     * add contact
     *
     * @param view
     */
    public void addContact(View view) {
        addContact();
    }

    public void addContact() {
        final String mStrContent = mContent.getText().toString();

        if (mStrContent==null||mStrContent.isEmpty()){
            new EaseAlertDialog(this, R.string.Please_enter_a_content).show();
        }
        if (EMClient.getInstance().getCurrentUser().equals(toAddUsername)) {
            new EaseAlertDialog(this, R.string.not_add_myself).show();
            return;
        }

        if (DemoHelper.getInstance().getContactList().containsKey(toAddUsername)) {
            //let the user know the contact already in your contact list
            if (EMClient.getInstance().contactManager().getBlackListUsernames().contains(toAddUsername)) {
                new EaseAlertDialog(this, R.string.user_already_in_contactlist).show();
                return;
            }
            new EaseAlertDialog(this, R.string.This_user_is_already_your_friend).show();
            return;
        }

        progressDialog = new ProgressDialog(this);
        String stri = getResources().getString(R.string.Is_sending_a_request);
        progressDialog.setMessage(stri);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

        new Thread(new Runnable() {
            public void run() {

                try {
                    //demo use a hardcode reason here, you need let user to input if you like
                    EMClient.getInstance().contactManager().addContact(toAddUsername, mStrContent);
                    runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.dismiss();
                            String s1 = getResources().getString(R.string.send_successful);
                            Toast.makeText(getApplicationContext(), s1, Toast.LENGTH_LONG).show();
                            finish();
                        }
                    });
                } catch (final Exception e) {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            progressDialog.dismiss();
                            String s2 = getResources().getString(R.string.Request_add_buddy_failure);
                            Toast.makeText(getApplicationContext(), s2 + e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });
                }
            }
        }).start();
    }
}
